package org.chy.config;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.chy.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author chenghy
 * @date 2018年01月15日 14时57分21秒
 * @Description 发上等愿,结中等缘,享下等福;向高处立,就平处坐,从宽处行.
 * @Package org.chy.config
 */
public class CustomUserDetails extends User implements UserDetails {

	private static final long serialVersionUID = 7123140094771225077L;

	private final boolean enabled;
	private final boolean accountNonExpired;
	private final boolean credentialsNonExpired;
	private final boolean accountNonLocked;
	private final Set<GrantedAuthority> authorities;
	
	public CustomUserDetails(User user,boolean enabled,
			boolean accountNonExpired, boolean credentialsNonExpired,
			boolean accountNonLocked, Collection<? extends GrantedAuthority>  authorities) {
		if (user != null 
				&& !StringUtils.isBlank(user.getUsername())
				&& !StringUtils.isBlank(user.getPassword())) {
			setUsername(user.getUsername());
			setPassword(user.getPassword());
			this.enabled = enabled;
			this.accountNonExpired = accountNonExpired;
			this.credentialsNonExpired = credentialsNonExpired;
			this.accountNonLocked = accountNonLocked;
			this.authorities =  Collections.unmodifiableSet(new HashSet<>(CollectionUtils.emptyIfNull(authorities)));
		} else {
			throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return null;
	}

	@Override
	public boolean isAccountNonExpired() {
		return false;
	}

	@Override
	public boolean isAccountNonLocked() {
		return false;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return false;
	}

	@Override
	public boolean isEnabled() {
		return false;
	}

}
